﻿using System;
using System.Runtime.InteropServices;

namespace StockLib.Data
{
    // 历史数据包 32 字节
    [StructLayout(LayoutKind.Sequential, Pack = 1)]
    public struct KFilePack
    {
        public Int32 m_time;               // UCT
        public Single m_fOpen;             // 开盘      
        public Single m_fHigh;             // 最高
        public Single m_fLow;              // 最低
        public Single m_fClose;            // 收盘
        public Single m_fVolume;           // 量
        public Single m_fAmount;           // 额
        public UInt16 m_wAdvance;          // 涨数,仅大盘有效
        public UInt16 m_wDecline;          // 跌数,仅大盘有效

        public static KFilePack FromInternal(KData kData)
        {
            KFilePack kPack = new KFilePack();
            kPack.m_time = kData.Time;
            kPack.m_fClose = kData.Close;
            kPack.m_fOpen = kData.Open;
            kPack.m_fHigh = kData.High;
            kPack.m_fLow = kData.Low;
            kPack.m_fVolume = kData.Volume;
            kPack.m_fAmount = kData.Amount;
            kPack.m_wAdvance = Convert.ToUInt16(kData.Advance);
            kPack.m_wDecline = Convert.ToUInt16(kData.Decline);

            return kPack;
        }

        public static KData ToInternal(ref KFilePack kPack)
        {
            KData kData = new KData();
            kData.Time = kPack.m_time;
            kData.Close = kPack.m_fClose;
            kData.Open = kPack.m_fOpen;
            kData.High = kPack.m_fHigh;
            kData.Low = kPack.m_fLow;
            kData.Volume = kPack.m_fVolume;
            kData.Amount = kPack.m_fAmount;
            kData.Advance = kPack.m_wAdvance;
            kData.Decline = kPack.m_wDecline;

            return kData;
        }
    };
}
